

function CanItemFitInContraband( id )

	local w = TS.ItemsData[id].Width;
	local h = TS.ItemsData[id].Height;

	local x, y = FindFreeSpaceInContraband( w, h );
	
	if( x ) then return true; end
	
	
	return false;

end

function GiveContrabandItem( id )

	local invtbl;

	if( type( id ) == "table" ) then

		invtbl = { }
	
		for k, v in pairs( id ) do
		
			invtbl[k] = v;
		
		end
		
		id = invtbl.ID;
	
	end
	
	local delay = 0;
	
	for k, v in pairs( player.GetAll() ) do
		
		if( v and v:IsValid() and v:IsPlayer() and v:GetTable().ContrabandMenu ) then
				
			if( not v:GetTable().ItemsDownloaded[id] ) then 
				v:SendItemData( id );
				delay = 1;
			end
				
		end
			
	end

	local w = TS.ItemsData[id].Width;
	local h = TS.ItemsData[id].Height;

	local x, y = FindFreeSpaceInContraband( w, h );

	if( x ) then

		InsertIntoContraband( x, y, w, h );
		
		if( invtbl ) then
			ContrabandGrid[x][y].ItemData = invtbl;
		else
			ContrabandGrid[x][y].ItemData = TS.ItemsData[id];
		end

		local isi = function()
		
		for k, v in pairs( player.GetAll() ) do
		
		if( v and v:IsValid() and v:IsPlayer() and v:GetTable().ContrabandMenu ) then

			umsg.Start( "ICI", v );
				umsg.String( id );
				umsg.Short( x );
				umsg.Short( y );
			umsg.End();
			end
		end

		end
		timer.Simple( delay, isi );
		
		return true;
		
	end
	

	
	return false;

end

function InsertIntoContraband( x, y, w, h )

	for k = 0, h - 1 do

		for j = 0, w - 1 do
		
			if( ContrabandGrid[x + j] and ContrabandGrid[x + j][y + k] ) then
				ContrabandGrid[x + j][y + k].Filled = true;
				ContrabandGrid[x + j][y + k].SX = x;
				ContrabandGrid[x + j][y + k].SY = y;

			end
			
		end
	
	end

end

function CanFitInContraband( x, y, w, h )

	local iw = 14
	local ih = 8

	for k = 0, h - 1 do
	
		for j = 0, w - 1 do
		
			if( x + j >= iw or
				y + k >= ih ) then
				
				return false;
				
			end
		
			if( not ContrabandGrid[x + j] or
				not ContrabandGrid[x + j][y + k] ) then

				return false;
				
			end
		
			if( ContrabandGrid[x + j][y + k].Filled ) then

				return false;
			
			end
		
		end
	
	end

	return true;
end

function FindFreeSpaceInContraband( w, h )

	local iw = 14
	local ih = 8
	
	for y = 0, ih - 1 do
	
		for x = 0, iw - 1 do

			if( ContrabandGrid[x] and ContrabandGrid[x][y] and ( not ContrabandGrid[x][y].Filled and CanFitInContraband( x, y, w, h ) ) ) then
				return x, y;
			
			end
		
		end
		
	end

	return false;

end

function RemoveContrabandItemAt( x, y )

	for k, v in pairs( ContrabandGrid ) do
	
		for n, m in pairs( v ) do

			if( m.SX == x and m.SY == y ) then
		
				ContrabandGrid[k][n].Filled = false;
				ContrabandGrid[k][n].SX = -1;
				ContrabandGrid[k][n].SY = -1;
				ContrabandGrid[k][n].ItemData = nil;
				
			end
		
		end
	
	end
	
		for k, v in pairs( player.GetAll() ) do
		
			if( v and v:IsValid() and v:IsPlayer() and v:GetTable().ContrabandMenu ) then
				umsg.Start( "RCI", v );
					umsg.Short( x );
					umsg.Short( y );
				umsg.End();
			end
			
		end


end

function HasContrabandItem( id )

	for i, v in pairs( ContrabandGrid ) do
	
		for x, c in pairs( v ) do
		
			for y, u in pairs( c ) do
				
				if( ContrabandGrid[x][y].ItemData ) then
				
					if( ContrabandGrid[x][y].ItemData.ID == id ) then
						
						return true, x, y;
						
					end
			
				end
			
			end
		
		end
	
	end
	
	return false;

end
